package com.example.demo;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/account")
@ResponseBody
public class AccountController {
	
	@Autowired
	private RedisService redisService;
	@Autowired
	private AccountService accountService;
	
	private String PRIFIX_USER_SESSION = "PRIFIX_USER_SESSION";
	
	@RequestMapping(value = "login", method = RequestMethod.POST)
	
	public ResourceSupport login(HttpServletRequest request, String username, String password) {
		Account accont = accountService.login(username, password);
		ResultResource result = new ResultResource();
		if (accont != null) {
			//TODO  save login info to redis and return
			String key = PRIFIX_USER_SESSION + ":" + accont.getName() + ":name";
			request.getSession().setAttribute(key, accont.getName());
			result.setSuccess(true);
			return result;
		} else {
			//TODO return failed result
			result.setSuccess(false);
			return result;
		}
	}

	
}
